﻿function AddCard(_id, _quantity) { 
    bl = true;
    
    if(_quantity == "") { _quantity = 1; }

    var productid = "";
    if (_id == "addchard_a_" + $("#" + _id).attr("itemid"))
        productid = $("#" + _id).attr("itemid");
    else
        productid = $("#txtProductId").val(); 
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_cartutility.aspx"),
        data    :   { otype: "addtocart", _itemid : productid,  qty: _quantity },
        async   :   false,
        success :   function(data, textStatus){
            if (data == "succeeded") {
                MiniCartContainer(document.getElementById(_id));
            }
            else if(data == "login") {
                window.location.href=prependOrgName("/membership_login.aspx");
            }
            else {
                bl=false;
            }
        }
    });
    
    return bl;
} 

function LoadingCartItems(tagObjectId)
{
    $.ajax({
        async: true,
        type: "POST",
        url:  prependOrgName("/shopping_cartutility.aspx"),
        data: {
            otype: "loadingcartitems"
        },
        beforeSend: function() {
            $("#"+tagObjectId).append("<tr><td colspan=\"6\" style=\"height: 50px; text-align: center;\"><img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" /></td></tr>");
            $("#"+tagObjectId).html("");
        },
        success: function(data, textStatus) {
            if(data == "login")
            { window.location.href=prependOrgName("/membership_login.aspx"); }
            else
            {
                $("#"+tagObjectId).html("");
                $("#"+tagObjectId).append(data);
                TotalCartAmount();
            }
        }
    });
}

function LoadingCartItemsView(tagObjectId)
{
    $.ajax({
        async: true,
        type: "POST",
        url:  prependOrgName("/shopping_cartutility.aspx"),
        data: {
            otype: "loadingcartitemsview"
        },
        beforeSend: function() {
            $("#"+tagObjectId).append("<tr><td colspan=\"4\" style=\"height: 50px; text-align: center;\"><img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" /></td></tr>");
            $("#"+tagObjectId).html("");
        },
        success: function(data, textStatus) {
            if(data == "login")
            {
                window.location.href=prependOrgName("/membership_login.aspx");
            }
            else
            {
                $("#"+tagObjectId).html("");
                $("#"+tagObjectId).append(data);
            }
        }
    });
}
  
function UpdateQty(_id)
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_cartutility.aspx"),
        data: { otype: "updateitemqty", _itemid: $("#" + _id).attr("itemid"), qty: $("#" + _id).val() },
        async   :   false,
        success :   function(data, textStatus){
            if(data == "succeeded"){
                $("#changeQtyText" + $("#" + _id).attr("itemid")).fadeIn();
                var _subtotal = parseFloat(Trim(_obj.value)) * parseFloat(Trim($("#spanPrice" + $("#" + _id).attr("itemid")).html()));

                $("#spanSubtotal" + $("#" + _id).attr("itemid")).html(_subtotal);
                TotalCartAmount();
                $("#changeQtyText" + $("#" + _id).attr("itemid")).fadeOut();
            }
            else {
                alert(data);
            }
        }
    });
}
 
function TotalCartAmount()
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_cartutility.aspx"),
        data    :   { otype: "totalcartamount"},
        async   :   false,
        success :   function(data, textStatus){
            $("#stCartItemsTotal").html(String(data)); 
        }
    });
}

function RemoveItem(_id, tagObjectId)
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_cartutility.aspx"),
        data: { otype: "removecartitem", _itemid: $("#" + _id).attr("itemid") },
        async   :   false,
        success :   function(data, textStatus){
            if(data == "succeeded"){
                 LoadingCartItems(tagObjectId);
            }
            else {
                alert(data);
            }
        }
    });
}
 
function ClearCartItems(tagObjectId)
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_cartutility.aspx"),
        data    :   { otype: "clearcartitems"},
        async   :   false,
        success :   function(data, textStatus){
            if(data == "succeeded"){
                 LoadingCartItems(tagObjectId);
                 $("#stCartItemsTotal").html("0.00");
            }
            else {
                alert(data);
            }
        }
    });
} 

function RemoveMiniCartItem(_id)
{
    $.ajax({
        type    :   "POST",
        url     :   prependOrgName("/shopping_cartutility.aspx"),
        data: { otype: "removecartitem", _itemid: $("#" + _id).attr("itemid") },
        async   :   false,
        success :   function(data, textStatus){
            if(data == "succeeded"){
                 $.ajax({
                    async: true,
                    type: "POST",
                    url:  prependOrgName("/shopping_cartutility.aspx"),
                    data: {
                        otype: "loadingminicartitems"
                    },
                    beforeSend: function() {
                        $("#divMiniCartContainer" + _currentCartContainer.split('|')[0]).html("<img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" />");
                    },
                    success: function(data, textStatus) {
                        if(data == "login")
                        {
                            
                        }
                        else
                        {
                            $("#divMiniCartContainer" + _currentCartContainer.split('|')[0]).html(data);
                        }
                    }
                });
            }
            else {
                alert(data);
            }
        }
    });
}

var _currentCartContainer = "";
function MiniCartContainer(_obj) { 
    if (_currentCartContainer != "")
        $("#divMiniCartContainer" + _currentCartContainer.split('|')[0]).remove();

    var selector = document.createElement("div");
    selector.id = "divMiniCartContainer" + _obj.itemid; 
    selector.itemid = _obj.itemid;
    selector.style.position = "absolute";
    selector.className = "cartCot";
    
    _obj.parentElement.appendChild(selector);
    _currentCartContainer = _obj.itemid + "|" + _obj.id;
     
    $.ajax({
        async: true,
        type: "POST",
        url:  prependOrgName("/shopping_cartutility.aspx"),
        data: {
            otype: "loadingminicartitems"
        },
        beforeSend: function() {
            $("#divMiniCartContainer"+_obj.itemid).html("<img src=" + prependOrgName("/_imgs/progress.gif") + " style=\"border: 0;\" />");
        },
        success: function(data, textStatus) {
            if(data == "login")
            {
                
            }
            else
            {
                $("#divMiniCartContainer"+_obj.itemid).html(data);
            }
        }
    });
}

document.onclick = function(event) {
    if(_currentCartContainer != "")
    {
        var e = event || window.event;
        var elem = e.srcElement || e.target;

        var _objId = _currentCartContainer.split('|')[1];
        var _itemid = _currentCartContainer.split('|')[0];
         
        while (elem) {
            if ((elem.id == _objId) || elem.id == ("divMiniCartContainer" + _itemid)) {
                return;
            }
            elem = elem.parentNode;
        }
        jQuery("#divMiniCartContainer" + _itemid).remove();
    }
}

